const resultEl = document.getElementById('result')
const inputEl = document.getElementById('input')
const buttonEl = document.getElementById('button')

const resultElV = document.getElementById('resultV')
const inputElV = document.getElementById('inputV')
const buttonElV = document.getElementById('buttonV')

var chars64 = '0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ@-'.split('');
var length64 = chars64.length;

// 10转64
buttonEl.addEventListener('click', get10to64)
inputEl.addEventListener('keydown', e => {
    if (e.key === 'Enter') {
      e.preventDefault()
      if (!inputEl.value.trim()) return
        get10to64()
    }
})

// 64转10
// buttonElV.addEventListener('click', get64to10)
// inputElV.addEventListener('keydown', e => {
//     if (e.key === 'Enter') {
//       e.preventDefault()
//       if (!inputEl.value.trim()) return
//        get64to10()
//     }
// })

// 10转64
function get10to64() {
    var dotArr;
    // if(inputEl.value.includes('.')){
        dotArr = inputEl.value.split('.') //小数两部分数组
    // }
    // 整数部分
    var number = Number(dotArr[0]);
    // 64进制数组
    var arr64 = [];
    var remainder;//余数
    while(number > 0){
        remainder = number % length64; //取余
        number = parseInt(number/length64); 
        arr64.unshift(chars64[remainder]);
    }

    // 小数部分
    // 64进制数组小数部分
    var arr64Dot = [];
    if(dotArr[1]){
        var numberDot = '0.'+ dotArr[1];
        var numb;//数
        var numbArr;    
        do{
            numb = numberDot * length64
            if(String(numb).includes('.')){
                numbArr = String(numb).split('.')
                arr64Dot.push(chars64[numbArr[0]])
                numberDot = '0.'+ numbArr[1]
            }else{
                arr64Dot.push(chars64[numb])
            }
            
        }while(String(numb).includes('.'))//是小数时，成64取整，算下去
        arr64Dot.unshift('.');
    }
     
    resultEl.innerHTML = arr64.join('') + arr64Dot.join(''); 
}

// // 64转10
// function get64to10() {
//     var number64Str = String(inputElV.value);
//     var len64 = number64Str.length;//64进制数长度
//     var i = 0;
//     var number10 = 0;//10进制数

//     while (i < len64) {

//         number10 += Math.pow(length64, i) * chars64.indexOf(number64Str.charAt(len64 - 1 - i));
//         i++;
//     }
//     resultElV.innerHTML = number10;
// }

